﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using System.Linq;

namespace SlotDuino.DAL
{
    public class BaseEntity : EntityObject, IDataErrorInfo
    {
        #region IDataErrorInfo implementation
        public string Error
        {
            get
            {
                ValidationContext vc = new ValidationContext(this, null, null);
                List<ValidationResult> result = new List<ValidationResult>();
                if (!Validator.TryValidateObject(this, vc, result))
                {
                    return result.First().ErrorMessage;
                }
                else
                {
                    return null;
                }
            }
        }

        public string this[string columnName]
        {
            get
            {
                ValidationContext vc = new ValidationContext(this, null, null);
                List<ValidationResult> result = new List<ValidationResult>();
                vc.MemberName = columnName;
                if (!Validator.TryValidateProperty(GetType().GetProperty(columnName).GetValue(this, null), vc, result))
                {
                    return result.First().ErrorMessage;
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion
    }
}
